package com.study.blogsearchconsumer.rabbitMQ;


import com.study.blogsearchconsumer.config.MqConfig;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;

/**
 * Mq发送方基础配置
 */
@Configuration
public class MqProducerBase {

    @Resource
    MqConfig mqCommonConfig;

    /**
     * @return
     */
    @Bean
    public ConnectionFactory jcMqConnectionFactory() {
        return MqProperties.getConnectionFactory(mqCommonConfig.jcRabbitAddresses, mqCommonConfig.jcRabbitUsername,
                mqCommonConfig.jcRabbitPassword, mqCommonConfig.jcRabbitVirtualHost);
    }

    /**
     * @return
     */
    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate jcRabbitTemplate(ConnectionFactory jcMqConnectionFactory) {
        return MqProperties.getRabbitTemplate(jcMqConnectionFactory);
    }

    /**
     * @return
     */
    @Bean
    public RabbitAdmin jcRabbitAdmin(ConnectionFactory jcMqConnectionFactory) {
        return MqProperties.getRabbitAdmin(jcMqConnectionFactory);
    }
//***********************************************************直连*******************************************************************************************
    // 队列
    @Bean("yanaDirectQueue0")
    public Queue yanaDirectQueue0(RabbitAdmin jcRabbitAdmin) {
       return MqProperties.getQueue(jcRabbitAdmin, "yanaDirectQueue0");
    }

    //2.声明直连交换机
    @Bean
    public DirectExchange yanaDirectExchange(RabbitAdmin jcRabbitAdmin) {
       return MqProperties.getDirectExchange(jcRabbitAdmin, "yanaDirectExchange");
    }

    //3.将声明的QueueBean和ExchangeBean建立绑定关系,设置路由key
    @Bean
    public Binding yanaDirectQueue0BindingDirectExchange(RabbitAdmin jcRabbitAdmin, Queue yanaDirectQueue0, DirectExchange yanaDirectExchange) {
       return MqProperties.bindDirectExchange(jcRabbitAdmin, yanaDirectExchange, yanaDirectQueue0, "yanaDirect");
    }
//***************************************************订阅Topic***************************************************************************************
    //申明队列
    @Bean("yanaTopicQueue1")
    public Queue yanaTopicQueue1(RabbitAdmin jcRabbitAdmin) {
//        return new Queue("yanaTopicQueue1", true, false, false);
        return MqProperties.getQueue(jcRabbitAdmin, "yanaTopicQueue1");
    }
//    队列
    @Bean("yanaTopicQueue2")
    public Queue yanaTopicQueue2(RabbitAdmin jcRabbitAdmin) {
        // return new Queue("yanaTopicQueue2", true, false, false);
        return MqProperties.getQueue(jcRabbitAdmin, "yanaTopicQueue2");
    }

    //2.声明订阅交换机
    @Bean
    public TopicExchange yanaTopicExchange(RabbitAdmin jcRabbitAdmin) {
        //return new TopicExchange("yanaTopicExchange", true, false);
        return MqProperties.getTopicExchange(jcRabbitAdmin, "yanaTopicExchange");
    }

    //3.将声明的topicQueueBean和topicExchangeBean建立绑定关系,设置路由key
    @Bean
    public Binding yanaTopicQueue1BindingTopicExchange(RabbitAdmin jcRabbitAdmin, Queue yanaTopicQueue1, TopicExchange yanaTopicExchange) {
        // return BindingBuilder.bind(yanaTopicQueue1).to(yanaTopicExchange).with("yanaTopic#").noargs();
        return MqProperties.bindTopicExchange(jcRabbitAdmin, yanaTopicExchange, yanaTopicQueue1, "yanaTopic#");
    }

    @Bean
    public Binding yanaTopicQueue2BindingTopicExchange(RabbitAdmin jcRabbitAdmin, Queue yanaTopicQueue2, TopicExchange yanaTopicExchange) {
        // return BindingBuilder.bind(yanaTopicQueue2).to(yanaTopicExchange).with("yanaTopic#").noargs();
        return MqProperties.bindTopicExchange(jcRabbitAdmin, yanaTopicExchange, yanaTopicQueue2, "yanaTopic#");
    }

    //********************************************广播Fanout*******************************************************************
    @Bean
    public FanoutExchange yanaFanoutExchange(RabbitAdmin jcRabbitAdmin) {
        return MqProperties.getFanoutExchange(jcRabbitAdmin, "yanaFanoutExchange");
    }

    //1.声明队列
    @Bean("yanaFanoutQueue3")
    public Queue yanaFanoutQueue3(RabbitAdmin jcRabbitAdmin) {
        return MqProperties.getQueue(jcRabbitAdmin, "yanaFanoutQueue3");
    }

    @Bean("yanaFanoutQueue4")
    public Queue yanaFanoutQueue4(RabbitAdmin jcRabbitAdmin) {
        return MqProperties.getQueue(jcRabbitAdmin, "yanaFanoutQueue4");
    }

    @Bean
    public Binding yanaTopicQueue1BindingFanoutExchange(RabbitAdmin jcRabbitAdmin,
                                                        FanoutExchange yanaFanoutExchange,
                                                        @Qualifier("yanaFanoutQueue3") Queue yanaFanoutQueue3) {
        return MqProperties.bindFanoutExchange(jcRabbitAdmin, yanaFanoutExchange, yanaFanoutQueue3);
    }

    @Bean
    public Binding yanaTopicQueue2BindingFanoutExchange(RabbitAdmin jcRabbitAdmin,
                                                        FanoutExchange yanaFanoutExchange,
                                                        @Qualifier("yanaFanoutQueue4") Queue yanaFanoutQueue4) {
        return MqProperties.bindFanoutExchange(jcRabbitAdmin, yanaFanoutExchange, yanaFanoutQueue4);
    }
}
